//
//  QDPickDocotrDateCell.m
//  simulateQuickDoctor
//
//  Created by 梁家伟 on 17/3/26.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDPickDocotrDateCell.h"

@interface QDPickDocotrDateCell ()

@property(nonatomic,weak)UILabel* dateLabel;
@property(nonatomic,weak)UILabel* detailDateLabel;
@property(nonatomic,weak)UILabel* feeLabel;
@property(nonatomic,weak)UILabel* countLabel;
@property(nonatomic,weak)UIButton* bookButtuon;

@end

@implementation QDPickDocotrDateCell

- (void)setupUI{
    
    //日期
    UILabel* dateLabel = [UILabel cz_labelWithText:@"2017-03-26" fontSize:16 color:[UIColor blackColor]];
    [self.contentView addSubview:dateLabel];
    
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(8);
        make.top.equalTo(self.contentView).offset(8);
    }];
    
    //星期几/上下午
    UILabel* detailDateLabel = [UILabel cz_labelWithText:@"星期日/下午" fontSize:16 color:[UIColor blackColor]];
    [self.contentView addSubview:detailDateLabel];
    
    [detailDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(dateLabel.mas_right).offset(32);
        make.top.equalTo(dateLabel);
    }];
    
    //坐诊费用
    UILabel* feeLabel = [UILabel cz_labelWithText:@"¥9.00" fontSize:14 color:[UIColor lightGrayColor]];
    [self.contentView addSubview:feeLabel];
    [feeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(dateLabel);
        make.top.equalTo(dateLabel.mas_bottom).offset(8);
        make.bottom.equalTo(self.contentView).offset(-8);
    }];
    
    //号源数量
    UILabel* countLabel = [UILabel cz_labelWithText:@"号源剩余:1" fontSize:14 color:[UIColor lightGrayColor]];
    [self.contentView addSubview:countLabel];
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(detailDateLabel);
        make.top.equalTo(feeLabel);
    }];
    
    UIButton* bookButton = [UIButton cz_textButton:@"可预约" fontSize:14 normalColor:PPColor selectedColor:PPColor];
    [self.contentView addSubview:bookButton];
    
    bookButton.layer.cornerRadius = 8;
    bookButton.layer.masksToBounds = YES;
    bookButton.layer.borderColor = PPColor.CGColor;
    bookButton.layer.borderWidth = 1;
    
    [bookButton setBackgroundColor:[UIColor whiteColor]];
    
    [bookButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-18);
        make.size.mas_equalTo(CGSizeMake(66, 36));
    }];
    
    
    _dateLabel = dateLabel;
    _detailDateLabel = detailDateLabel;
    _countLabel = countLabel;
    _feeLabel = feeLabel;
    _bookButtuon = bookButton;
    
    
    [bookButton addTarget:self action:@selector(changeBookStatus:) forControlEvents:UIControlEventTouchUpInside];
}


- (void)changeBookStatus:(UIButton*)sender{
    
    self.model.isCalled = !self.model.isCalled;
    
    if(self.model.isCalled){
        
        [_bookButtuon setBackgroundColor:PPColor];
        [_bookButtuon setTitle:@"已预约" forState:UIControlStateNormal];
        [_bookButtuon setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        self.model.has_number--;
        
    }else{
        
        [_bookButtuon setBackgroundColor:[UIColor whiteColor]];
        [_bookButtuon setTitleColor:PPColor forState:UIControlStateNormal];
        [_bookButtuon setTitle:@"可预约" forState:UIControlStateNormal];
        
        self.model.has_number++;
    }
    
    _countLabel.text = [NSString stringWithFormat:@"号源剩余:%zd",_model.has_number];
}


- (void)setModel:(QDDutyModel *)model{
    
    _model = model;
    
    _dateLabel.text = _model.duty_date;
    _detailDateLabel.text = [NSString stringWithFormat:@"%@/%@",[self getDetailDate:_model.duty_date],_model.offer_time];
    _feeLabel.text = [NSString stringWithFormat:@"¥%.2f",_model.zuozhen_fee];
    _countLabel.text = [NSString stringWithFormat:@"号源剩余:%zd",_model.has_number];
    
   
    
    if(_model.isCalled){
        
        [_bookButtuon setBackgroundColor:PPColor];
        [_bookButtuon setTitle:@"已预约" forState:UIControlStateNormal];
        [_bookButtuon setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }else{
        
        [_bookButtuon setBackgroundColor:[UIColor whiteColor]];
        [_bookButtuon setTitleColor:PPColor forState:UIControlStateNormal];
        
        if(_model.has_number == 0){
            _bookButtuon.enabled = NO;
            [_bookButtuon setTitle:@"无号" forState:UIControlStateNormal];
            [_bookButtuon setBackgroundColor:[UIColor lightGrayColor]];
        }else{
            [_bookButtuon setTitle:@"可预约" forState:UIControlStateNormal];
        }
    }
    
}

- (NSString*)getDetailDate:(NSString*)dateString{
    NSString* weekday = nil;
    
    NSDateFormatter* formatter = [NSDateFormatter new];
    formatter.dateFormat = @"yyyy-MM-dd";
    
    NSDate* date = [formatter dateFromString:dateString];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents * components = [calendar components:NSCalendarUnitWeekday fromDate:date];
    
    switch (components.weekday) {
        case 1:
            weekday = @"星期天";
            break;
        case 2:
            weekday = @"星期一";
            break;
        case 3:
            weekday = @"星期二";
            break;
        case 4:
            weekday = @"星期三";
            break;
        case 5:
            weekday = @"星期四";
            break;
        case 6:
            weekday = @"星期五";
            break;
        case 7:
            weekday = @"星期六";
            break;
    }
    
    return weekday;
}

@end
